using System;
using System.Collections;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;

namespace DAO
{
    public static class TextBaseFactory
    {
        public static Dictionary<string, string> dctTextBase = new Dictionary<string, string>();
        public static string loadText(string Reference, Int64 langId)
        {
            // validate input params
            if (Reference.Length < 1 || langId < 1)
                return String.Empty;

            //tuka naprai proverka dali referencata ja ima vo dict
            string outString;
            if (dctTextBase.TryGetValue(Reference + langId.ToString(), out outString))
                return outString;

            // depending on the type of object generate the appropriate SQL query
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand command = new SqlCommand("SELECT * from dbo.Fn_GetTextRefference(@lang_id, @refference)", conn))
                {
                    command.Parameters.Add(
                        new SqlParameter("lang_id", langId));
                    command.Parameters.Add(
                        new SqlParameter("refference", Reference));
                    // create the command using Nhibernate's connection            

                    string tmpStr = (string)command.ExecuteScalar();


                    if (tmpStr == "" || tmpStr == null)
                        return "";
                    else
                    {
                        dctTextBase.Add(Reference + langId.ToString(), tmpStr);
                        return tmpStr;
                    }
                }
            }
        }

        public static string loadInfoText(string Reference, Int64 langId)
        {
            // validate input params
            if (Reference.Length < 1 || langId < 1)
                return String.Empty;
            
            // depending on the type of object generate the appropriate SQL query
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand command = new SqlCommand("SELECT * from dbo.Fn_GetTextInfoRef(@lang_id, @refference)", conn))
                {
                    command.Parameters.Add(
                        new SqlParameter("lang_id", langId));
                    command.Parameters.Add(
                        new SqlParameter("refference", Reference));
                    // create the command using Nhibernate's connection            

                    string tmpStr = (string)command.ExecuteScalar();


                    if (tmpStr == "" || tmpStr == null)
                        return "";
                    else
                    {
                        return tmpStr;
                    }
                }
            }
        }

        public static void updateDictionary(string Reference, Int64 langId, string newstr)
        {
            string outString;
            if (dctTextBase.TryGetValue(Reference + langId.ToString(), out outString))
            {
                dctTextBase[Reference + langId.ToString()] = newstr;
            }
        }
    }
}

